import java.awt.Graphics2D;
import java.util.Vector;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Font;

/**
 * Class for creating a text object
 * 
 * @author Chia-Chi (Teresa) Lee
 */
public class Text extends MyShape {
  
  //Store a list of characters
  private Vector chars = new Vector();
  //Store the font family; default font is Dialog
  private String myFont ="Dialog";
  
  
  public Text(int x_s, int y_s){
    setX_s(x_s);
    setY_s(y_s);
  }
  
  /**
   * Set the font of the text object
   * 
   * @param font font family in string representation
   */
  public void setFont(String font){
    myFont = font;
  }
  
  /**
   * Add a character to the list
   * 
   * @param character to be added
   */
  @Override
  public void add(char c){
   chars.add(c); 
  }
  
  /**
   * Get size of the Vector chars
   * 
   * @return the size of Vector chars
   */
  public int getSize(){
   return chars.size(); 
  }
  
  /**
   * Remove a character from Vector chars
   * 
   * @param the index of character to be removed
   */
  public void remove(int index){
   chars.remove(index);
  }
  
  /**
   * Method for drawing a text object
   * 
   */
  @Override
  public void drawMe(Graphics2D g, int screenWidth, int screenHeight) {
    
    //Set the font family
    g.setFont(new Font(myFont, Font.PLAIN, 12));
    
    //Convert Vector to String
    String s = "";
    for(int i=0; i<chars.size(); i++){
      s += chars.get(i);
    }
    
    //Find the width of the text object
    FontMetrics fontMetrics = g.getFontMetrics();
    int width = fontMetrics.stringWidth(s);
    
    //The current position of text input cursor 
    int curr_x = this.getX_s();
    int curr_y = this.getY_s();
    
    //Use for storing text object width
    int stringWidth = 0;
    
    //Use in the loop for storing the start index
    int startIndex = 0;
    
    //Store the index of a space
    int spaceIndex = 0;
    
    //Use for temporarily storing a string
    String aString = "";
    
    //Drawing the text object
    //First, draw any text that are not in the last line
    for(int i=0; i < s.length(); i++){
      
      //Check if a substring exceeds the window size
      aString = s.substring(startIndex, i);
      stringWidth = fontMetrics.stringWidth(aString);
      
      //Do something if a substring exceeds a window size; else, do nothing
      if((curr_x + stringWidth) >= screenWidth - 20){
        
        //Check if the substring has a space
        spaceIndex = s.lastIndexOf(" ",i);
        //If the substring has space, break at space
        if(spaceIndex != -1 && spaceIndex >= startIndex){
          aString = s.substring(startIndex, spaceIndex);
          g.drawString(aString, curr_x, curr_y);
          i = spaceIndex+1;
          startIndex = spaceIndex+1;
        }
        //If the substring has no space, break at i
        else{
          aString = s.substring(startIndex, i);
          g.drawString(aString, curr_x, curr_y);
          startIndex = i;
        }
        
        //Set the text input cursor to the beginning
        curr_x = 10;
        curr_y += 30;
      }
  }

  //Draw the last line of text
  aString = s.substring(startIndex);
  g.drawString(aString, curr_x, curr_y);
      
  
  }
  
}